/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Enumeration;

import com.zxy.business.Analyse_Data;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class ServerHandler extends Thread
{
	private Socket			mServSocket;
	//序号
	private int				iNum;
	public ServerHandler(Socket clientSocket,int Num)
	{
		// 得到socket连接
		mServSocket = clientSocket;
		iNum = Num;
	}

	private void init()
	{
		/** 解析收到数据 **/
		Analyse_Data ad = new Analyse_Data();
		ad.iNum = this.iNum;
		byte[] bytNew = null;
		byte[] Bytes = null;
		InputStream isIn = null;
		try
		{
			if(mServSocket.isConnected())
			{
				Com_Para.htServSockets.put("servSocket"
						+ mServSocket.getInetAddress().getHostAddress()+"|"+String.valueOf(mServSocket.getPort()),mServSocket);
				System.out.println("客户端子设备连接："
						+ mServSocket.getInetAddress().getHostAddress()+"|"+String.valueOf(mServSocket.getPort()) + " "
						+ Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS"));
			}		
			while(!java.util.Objects.equals(mServSocket,null)
					&& mServSocket.isConnected() && !mServSocket.isClosed())
			{
				//以char[]形式接收
//				isReader = new InputStreamReader(mServSocket.getInputStream(),Com_Para.U_CODE);
//				reader = new BufferedReader(isReader);
//				mServSocket.setReceiveBufferSize(1024 * 50);
//				// 设置缓冲区大小
//				char[] Bytes = new char[mServSocket.getReceiveBufferSize()];
//				int iLength = reader.read(Bytes,0,Bytes.length);
				
				//以byte[]形式接收
				isIn = mServSocket.getInputStream();
				Bytes = new byte[mServSocket.getReceiveBufferSize()];
				int iLength = isIn.read(Bytes,0,Bytes.length);
				
				if(iLength > 0)
				{
//					char[] bytNew = new char[iLength];
//					System.arraycopy(Bytes, 0, bytNew, 0,iLength);
//					String strIP = mServSocket.getInetAddress().getHostAddress()+"|"+String.valueOf(mServSocket.getPort());// "0.0.0.0";
//					/** 解析收到数据 **/
//					Analyse_Data ad = new Analyse_Data();
//					ad.SubAnalyseRecBytes(iLength,bytNew,mServSocket,true,strIP);
					
					bytNew = new byte[iLength];
					System.arraycopy(Bytes, 0, bytNew, 0,iLength);	
					String strIP = mServSocket.getInetAddress().getHostAddress()+"|"+String.valueOf(mServSocket.getPort());// "0.0.0.0";
					ad.SubAnalyseRecBytes(iLength,bytNew,mServSocket,true,strIP);					
					Thread.sleep(100);
				}
				
				Com_Fun.SendSocket(new char[] {'\r','\n'},mServSocket);
			}
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString(),"客户端链接已关闭链接"
					+ mServSocket.getInetAddress().getHostAddress()+"|"+String.valueOf(mServSocket.getPort())
					+ e.getMessage());
			Com_Para.htServSockets.remove("servSocket"
					+ mServSocket.getInetAddress().getHostAddress()+"|"+String.valueOf(mServSocket.getPort()));
			String TemChildNode = "";
			for(Enumeration<String> sm = Com_Para.htChildNode.keys();sm.hasMoreElements();)
			{
				TemChildNode = sm.nextElement();
				if(!java.util.Objects.equals(Com_Para.htChildNode.get(TemChildNode),null)
						&& Com_Para.htChildNode.get(TemChildNode).toString().equals("servSocket"
								+ mServSocket.getInetAddress().getHostAddress()))
				{
					break;
				}
			}
			try
			{
				isIn.close();
				mServSocket.close();
				mServSocket = null;
			}
			catch(Exception e1)
			{
			}
		}
		Thread.yield();
		System.gc();
	}

	public void run()
	{
		this.setName("ServerHandler_"+mServSocket.getInetAddress().getHostAddress()+"|"+String.valueOf(mServSocket.getPort()));
		init();
	}
}